package com.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Zhengjz
 * @version 1.0
 * @className ShiroConfig
 * @date 2021/1/10 14:33
 */
@Configuration
public class ShiroConfig {



    //shiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("DefaultWebSecurityManager") DefaultWebSecurityManager DefaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean =  new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(DefaultWebSecurityManager);

        //添加shiro内置过滤器
        /**
         *  anon 无需认证就可以访问
         *  authc 必须认证了才可以访问
         *  user 必须有记住我功能才可以用
         *  perms 拥有对某个资源的权限才能访问
         *  role 拥有某个角色的权限
         */
        //拦截
        Map filterMap = new LinkedHashMap();
      //  filterMap.put("/login","authc");
      //  filterMap.put("/user/update","authc");
        //设置一个过滤去的链
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        //设置登录的页面
        shiroFilterFactoryBean.setLoginUrl("/tologin");
        //设置必须授权才能访问的页面
      //  filterMap.put("/login","perms[user:main]");
//        filterMap.put("/user/update","perms[user:update]");
//        filterMap.put("/member/delete","perms[user:update]");
        //设置未授权页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/natuch");


        return shiroFilterFactoryBean;
    }


    //DefaultWebSecurityManager对象
    @Bean(name = "DefaultWebSecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("UserRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }

    //realm对象
    @Bean(name = "UserRealm")
    public UserRealm getUserRealm(){
        return new UserRealm();
    }



    //整合ShiroDialect 用来整合shiro thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }


}
